package com.xuelangyun.form.sys.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xuelangyun.form.common.cache.annotation.TrCacheConfig;
import com.xuelangyun.form.common.redis.ICacheFilter;
import com.xuelangyun.form.common.security.AuthSecurityUtils;
import com.xuelangyun.form.common.service.CustomCacheServiceImpl;
import com.xuelangyun.form.common.utils.PageUtils;
import com.xuelangyun.form.common.utils.Query;
import com.xuelangyun.form.sys.dao.ISysDictDao;
import com.xuelangyun.form.sys.entity.SysDictEntity;
import com.xuelangyun.form.sys.service.ISysDictService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Arrays;
import java.util.Collection;
import java.util.Date;
import java.util.List;


/**
 * SysDictServiceImpl
 *
 * @author weiqing.huang
 * @version 1.0
 * @date 2024年7月18日17:57:17
 */
@Service("sysDictService")
public class SysDictServiceImplCustom extends CustomCacheServiceImpl<ISysDictDao, SysDictEntity> implements ISysDictService {

    @Autowired
    private AuthSecurityUtils authUtils;

    @Override
    @TrCacheConfig("'selectByTypeValue.' + #p0 + '.' + com.xuelangyun.form.common.utils.collectionsToString(#p1)")
    public List<SysDictEntity> selectByTypeValue(String type, List<String> value) {

        QueryWrapper<SysDictEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        wrapper.eq("delete_flag", 0);
        wrapper.in("value", value);
        return this.list(wrapper);
    }

    public PageUtils queryTypePage(String type, PageUtils page) {
        IPage<SysDictEntity> ipage = baseMapper.queryPageList(page.getPage(SysDictEntity.class), type);
        return new PageUtils(ipage);
    }

    @Override
    public PageUtils queryPage(SysDictEntity entity, PageUtils page) {

        String type = entity.getType();
        String name = entity.getName();

        QueryWrapper<SysDictEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("delete_flag", 0);
        if (StringUtils.isNotBlank(type)) {
            wrapper.eq("type", type);
        } // .or().like("dict_name", key).or().like("description", key);
        if (StringUtils.isNotBlank(name)) {
            wrapper.like("name", name);
        }
        IPage<SysDictEntity> ipage = this.page(new Query<SysDictEntity>(page).getPage(), wrapper);
        return new PageUtils(ipage);
    }

    @Override
    @TrCacheConfig("'byTypeDictValue.' + #p0 + '.' + #p1")
    public SysDictEntity findByTypeDictValue(String type, String value) {

        QueryWrapper<SysDictEntity> skuEntityWrappers = new QueryWrapper<>();
        skuEntityWrappers.eq("type", type).eq("value", value);
        skuEntityWrappers.eq("delete_flag", 0);
        return this.getOne(skuEntityWrappers, false);
    }

    @Override
    public boolean save(SysDictEntity entity) {
        // TODO Auto-generated method stub
        if (entity.getCreateTime() == null) {
            entity.setCreateTime(new Date());
            entity.setUpdateTime(new Date());
        }

        if (StringUtils.isBlank(entity.getCreateId())) {
            String userId = authUtils.getCurrentUser().getUserId();
            entity.setCreateId(userId);
            entity.setUpdateId(userId);
        }
        entity.setDeleteFlag(0);
        return super.save(entity);
    }

    @Override
    //@TrCacheEvict("#className + '.' + #p0.dictId")
    public boolean updateById(SysDictEntity entity) {

        entity.setUpdateTime(new Date());
        String userId = authUtils.getCurrentUser().getUserId();
        entity.setUpdateId(userId);
        return super.updateById(entity);
    }


    @Override
    public boolean saveBatch(Collection<SysDictEntity> entityList, int batchSize) {
        clearCache();
        return super.saveBatch(entityList, batchSize);
    }

    @Override
    @TrCacheConfig("'findByType.' + #p0")
    public List<SysDictEntity> findByType(String type) {
        QueryWrapper<SysDictEntity> wrapper = new QueryWrapper<>();
        wrapper.eq("type", type);
        wrapper.eq("delete_flag", 0);
        return list(wrapper);
    }


    @Override
    public String findByTypeAndValue(String type, String value) {
        StringBuffer stringBuffer = new StringBuffer();
        if (StringUtils.isNotBlank(value)) {
            List<SysDictEntity> sysDictEntities = this.selectByTypeValue(type, Arrays.asList(value.split(",")));
            for (SysDictEntity dict : sysDictEntities) {
                stringBuffer.append(dict.getName() + ",");
            }
        }
        if (stringBuffer.toString().length() == 0) {
            return "";
        }
        return stringBuffer.toString().substring(0, stringBuffer.toString().length() - 1);
    }

    @Override
    public void updateTypeByEntity(SysDictEntity entity) {
        // TODO Auto-generated method stub
        QueryWrapper<SysDictEntity> query = new QueryWrapper<>();
        query.eq("type", entity.getId());

        SysDictEntity update = new SysDictEntity();
        update.setType(entity.getType());
        update.setTypeName(entity.getTypeName());
        update(update, query);
    }

}

